var app = new Vue({
    el: '#app',
    data: {
        buildingDetail: {
            id: 0
        },
        floorList: {},
        dialog: {
            isShow: false,
            disabled: false
        },
        roomDetail: {},
        picsDialog: {
            isShow: false,
            data: [],
            height: ''
        }
    },
    mounted:function(){
        this.buildingDetail.id = this.getUrlKey('id');
        this.getBuildingDetail()
    },
    methods: {
        /**
         * 获取楼栋详情
         */
        getBuildingDetail: function() {
            var _this = this;
            $.ajax({
                url: '/building/getOne',
                type: 'get',
                data: _this.buildingDetail,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                       _this.buildingDetail = res.data.building;
                       _this.floorList = res.data.floor;
                    } else {
                        _this.$message.error(res.message)
                    }
                }
            })
        },
        /**
         * 显示楼栋信息
         * @param row
         */
        showBuildingDetail: function(row) {
            var _this = this;
            /*if (row.id) {
                _this.getBuildingDetail(row.id, function(res) {
                    _this.buildingDetail = res.data;
                    _this.dialog.isShow = true;
                })
            } else {
                _this.buildingDetail = row;
                _this.dialog.isShow = true;
            }*/
            _this.buildingDetail = row;
            _this.dialog.isShow = true;
        },
        /**
         * 楼栋保存
         * @returns {boolean}
         */
        buildingSave: function() {
            var _this = this;
            if (!_this.buildingDetail.build_num) {
                _this.$message.error('请输入楼栋编号');
                return false;
            }
            if (!_this.buildingDetail.name) {
                _this.$message.error('请输入楼栋名称');
                return false;
            }
            if (!_this.buildingDetail.floor_num) {
                _this.$message.error('请输入楼栋楼层数');
                return false;
            }
            _this.dialog.disabled = true;
            _this.buildingDetail.community_id = _this.community.id;
            $.ajax({
                url: '/building/save',
                type: 'post',
                data: _this.buildingDetail,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        _this.buildingDetail.id = res.data.id;
                        _this.$message.success(res.message);
                    } else {
                        _this.$message.error(res.message)
                    }
                    _this.dialog.disabled = false;
                }
            })
        },
        /**
         * 楼层新增
         */
        floorAdd: function() {
            var floor_num = this.floorList.length > 0 ? parseInt(this.floorList[this.floorList.length - 1].floor_num) + 1 : 1;
            this.floorList.push({
                floor_num: floor_num,
            })
        },
        /**
         * 房间新增
         * @param floor_num
         */
        roomAdd: function(floor_num) {
            this.dialog.isShow = true;
            this.dialog.title = '新增房间';
            this.roomDetail = {};
            this.$set(this.roomDetail, 'floor_num', floor_num)
        },
        /**
         * 房间保存
         */
        roomSave: function() {
            var _this = this;
            if (!_this.roomDetail.room_num) {
                _this.$message.error('请输入房间编号');
                return false;
            }
            if (!_this.roomDetail.status) {
                _this.$message.error('请输入房间状态');
                return false;
            }
            _this.dialog.disabled = true;
            _this.roomDetail.build_id = _this.buildingDetail.id;
            $.ajax({
                url: '/building/roomSave',
                type: 'post',
                data: _this.roomDetail,
                dataType: 'json',
                success: function(res) {
                    if (res.status === 0) {
                        // 重新拉取房间信息
                        _this.getBuildingDetail();
                        _this.dialog.isShow = false;
                        _this.$message.success(res.message);
                    } else {
                        _this.$message.error(res.message)
                    }
                    _this.dialog.disabled = false;
                }
            })
        },
        /**
         * 显示房间信息
         * @param room
         */
        showRoomDetail: function(room) {
            this.roomDetail = room;
            this.dialog.isShow = true;
            this.dialog.title = '房间信息';
        },
        /**
         * 上传成功
         */
        fileSuccess: function(response, file, fileList, field) {
            var _this = this;
            if (response.status === 0) {
                file.id = response.data.id;
                file.raw = {};
                _this.roomDetail[field] = fileList;
                _this.$message.success(response.message)
            } else {
                _this.$message.error(response.message)
            }
        },
        /**
         * 图片预览
         */
        filePreview: function(file) {
            var _this = this;
            this.picsDialog.data = this.roomDetail.pics;
            // 获取图片的最大高度
            _this.picsDialog.height = 0;
            this.picsDialog.data.map(function(r){
                var img = new Image();
                img.src = r.url;
                var height = document.body.clientWidth / img.width * img.height;
                if (height > _this.picsDialog.height) {
                    _this.picsDialog.height = height;
                }
            });
            _this.picsDialog.height += 'px';
            this.picsDialog.isShow = true;
        },
        /**
         * 图片删除
         */
        fileRemove: function(file, fileList, field) {
            var _this = this;
            _this.roomDetail[field] = fileList;
            _this.$message.success('删除成功！')
        },
        fileExceed: function() {
            this.$message.warning('只允许上传一个文件！');
        },
        imgDetail: function(url) {
            window.location.href = url;
        }
    }
});
